//
//  StarCooperationViewController.m
//  LiveStar
//
//  Created by 漪珊 on 2017/2/17.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "StarCooperationViewController.h"
#import "StarCooperationCell.h"
#import "StarCooperationModel.h"

@interface StarCooperationViewController ()<UITableViewDelegate,UITableViewDataSource,StarCooperationCellDelegate>{
    NSMutableArray *_cooperationArray;
}
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation StarCooperationViewController

#pragma mark -- 网络请求
- (void)StarCooperationListRequest {
    NSDictionary *dict = @{@"udid":User_ID,
                           @"token":TOKEN,
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kStarCooperation];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            NSArray *array = responseObject[@"data"];
            for (NSDictionary *cooperDict in array) {
                StarCooperationModel *model = [[StarCooperationModel alloc] initWithDict:cooperDict];
                [_cooperationArray addObject:model];
            }
            [self.tableView reloadData];
        }
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self StarCooperationListRequest];
    [self setUpUI];
    // Do any additional setup after loading the view.
}

#pragma mark -- UI
- (void)setUpUI {
    _cooperationArray = [NSMutableArray arrayWithCapacity:1];
    self.title = @"商家礼品赞助";
    //tableView
    [self creatTableView];
    
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.tableView];
}

#pragma mark -- tableView 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _cooperationArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    StarCooperationCell *cell = [StarCooperationCell loadNibCellWithTabelView:tableView];
    cell.cooperModel = _cooperationArray[indexPath.row];
    cell.delegate = self;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


#pragma mark -- 区头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *sectionView = [[UIView alloc] init];
    sectionView.backgroundColor = HESHEX_COLOR(@"FF9999");
    UILabel *label = [[UILabel alloc] init];
    label.textColor = [UIColor whiteColor];
    label.text = @"为您推送的商家都已经经过官方核实资质，请放心合作";
    label.font = UIFont(11);
    [sectionView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(sectionView);
        make.left.equalTo(sectionView.mas_left).offset(15);
    }];
    
    return sectionView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}


#pragma mark -- StarCooperationDelegate
- (void)phoneWithCell:(StarCooperationCell *)cooperationCell {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cooperationCell];
    StarCooperationModel *model = _cooperationArray[indexPath.row];
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",model.g_phone];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
